<?php

namespace App\Http\Controllers\Service;

use App\Entity\Member;
use App\Entity\TempEmail;
use App\Entity\TempPhone;
use App\Http\Controllers\Controller;
use App\Models\M3Email;
use App\Models\M3Result;
use App\Tool\UUID;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class MemberController extends Controller
{
    //register
    public function register(Request $request)
    {
        $m3Result = new M3Result();
        $phone = $request->input('phone', '');
        $phone_code = $request->input('phone_code','');
        $email = $request->input('email', '');
        $password = $request->input('password', '');
        $confirm = $request->input('confirm', '');
        $validate_code = $request->input('validate_code', '');


        if ($phone == '' && $email == '') {
            $m3Result->status = 1;
            $m3Result->message = '手机号或邮箱不能为空';
            return $m3Result->toJson();
        }

        if ($password == "" || strlen($password) < 6) {
            $m3Result->status = 2;
            $m3Result->message = '密码不能少于6位';
            return $m3Result->toJson();
        }


        if ($confirm == "" || strlen($confirm) < 6) {
            $m3Result->status = 3;
            $m3Result->message = '确认密码不能少于6位';
            return $m3Result->toJson();
        }

        if ($password != $confirm) {
            $m3Result->status = 4;
            $m3Result->message = '密码和确认密码不相同';
            return $m3Result->toJson();
        }


        if ($phone != null) {

            //验证手机验证码格式对与否
            if ($phone_code == '' || strlen($phone_code) != 4) {
                $m3Result->status = 5;
                $m3Result->message = '手机验证码为4位';
                return $m3Result->toJson();
            }

            //验证手机验证码是否正确
            $tempPhone = TempPhone::query()->where('phone', $phone)->first();

            if ($tempPhone->code == $phone_code) {

                $deadline = $tempPhone->deadline;
                date_default_timezone_set("PRC");
                if (time() > strtotime($deadline)) {
                    $m3Result->status = 6;
                    $m3Result->message = '手机验证码已过期';
                    return $m3Result->toJson();
                }


                $member = new Member();
                $member->phone = $phone;
                $member->password = md5('bk'.$password);
                $member->save();

                $m3Result->status = 0;
                $m3Result->message = '注册成功';

            } else {
                $m3Result->status = 7;
                $m3Result->message = '手机验证码不正确';
                return $m3Result->toJson();
            }

        } else {

            if ($validate_code == '' || strlen($validate_code) != 4) {
                $m3Result->status = 6;
                $m3Result->message = '验证码为4位';
                return $m3Result->toJson();
            }

            $s_validate_code = $request->session()->get('validate_code', '');

            if ($s_validate_code != $validate_code) {
                $m3Result->status = 4;
                $m3Result->message = '验证码不正确';
                return $m3Result->toJson();
            }


            $member = Member::query()->where('email',$email)->first();


            if ($member != null && $member->active == 1) {
                $m3Result -> status = 3;
                $m3Result -> message = '邮箱已注册';
                return $m3Result->toJson();
            }

            if ($member == null) {
                $member = new Member();
                $member -> email = $email;
                $member -> password = md5('bk'.$password);
                $member->save();
            }

            $uuid = UUID::create();

            $m3Email = new M3Email();
            $m3Email->to = $email;
            $m3Email->cc = '787038873@qq.com';
            $m3Email->content = '请于24小时点击该链接完成验证. http://localhost:8000/service/validate_email'
                . '?email=' . $member->email
                . '&code=' . $uuid;


            $tempEmail = TempEmail::query()->where('email', $email)->first();

            if ($tempEmail == null)
                $tempEmail = new TempEmail();


            $tempEmail->email = $email;
            $tempEmail->code = $uuid;
            $tempEmail->deadline = date('Y-m-d H:i:s',time() + 24*60*60);
            $tempEmail->save();

            Mail::send('email_register', ['m3Email' => $m3Email], function ($m) use ($m3Email) {
                // $m->from('hello@app.com', 'Your Application');
                $m->to($m3Email->to, '尊敬的用户')
                    ->cc($m3Email->cc)
                    ->subject($m3Email->subject);
            });

            $m3Result->status = 0;
            $m3Result->message = '等待邮箱验证';

        }




        return $m3Result->toJson();
    }
    //login
    public function login(Request $request)
    {
        $m3Result = new M3Result();
        $username = $request->input('username','');
        $password = $request->input('password','');
        $validate_code = $request->input('validate_code','');

        //验证格式

        //比对验证码
        $session_code = $request->session()->get('validate_code');
        if ($session_code != $validate_code) {
            $m3Result->status = 1;
            $m3Result->message = '验证码错误';
            return $m3Result->toJson();
        }

        $member = new Member();

        if (strpos($username,'@')) {
            $member = Member::query()->where('email', $username)->first();
        } else {
            $member = Member::query()->where('phone', $username)->first();
        }

        if ($member == null) {
            $m3Result->status = 1;
            $m3Result->message = '用户不存在';
            return $m3Result->toJson();
        }

        if ($member->password != md5('bk'.$password)) {
            $m3Result->status = 1;
            $m3Result->message = '密码不正确';
            return $m3Result->toJson();
        }

        $request->session()->put('member',$member);

        $m3Result->status = 0;
        $m3Result->message = '登录成功';
        return $m3Result->toJson();
    }
}
